module("Matrix");

function matrixAlmostEqual(actual, expected, delta) {
    var diff = Matrix.maxDifference(actual, expected);
    almostEqual(diff, 0, delta);
}

test("Create matrix", function() {
        var m = new Matrix(1, 2, 3, 4, 5, 6, 7, 8, 9);
        equal(m.xx, 1);
        equal(m.xy, 2);
        equal(m.xz, 3);
        equal(m.yx, 4);
        equal(m.yy, 5);
        equal(m.yz, 6);
        equal(m.zx, 7);
        equal(m.zy, 8);
        equal(m.zz, 9);
});

test("Add", function() {
	var a = new Matrix(1, 2, 3, 4, 5, 6, 7, 8, 9);
	var b = new Matrix(2, 3, 4, 5, 6, 7, 8, 9, 10);
	var sum = Matrix.add(a, b);
        equal(sum.xx, 3);
        equal(sum.xy, 5);
        equal(sum.xz, 7);
        equal(sum.yx, 9);
        equal(sum.yy, 11);
        equal(sum.yz, 13);
        equal(sum.zx, 15);
        equal(sum.zy, 17);
        equal(sum.zz, 19);
});

test("Absolute max positive", function() {
	var m = new Matrix(1, 2, 3, 4, 9, 5, 6, 7, 8);
	almostEqual(m.absoluteMax(), 9);
});

test("Absolute max negative", function() {
	var m = new Matrix(1, 2, 3, 4, -9, 5, 6, 7, 8);
	almostEqual(m.absoluteMax(), 9);
});

test("Scalar multiplication", function() {
	var m = new Matrix(1, 2, 3,
			   4, 5, 6,
			   7, 8, 9);
	var actual = m.multiplyScalar(3);
	var expected = new Matrix(3,  6,  9,
				  12, 15, 18,
				  21, 24, 27);
	matrixAlmostEqual(actual, expected);
});

test("In-place scalar multiplication", function() {
	var actual = new Matrix(1, 2, 3,
				4, 5, 6,
				7, 8, 9);
	actual.multiplyScalarInPlace(3);
	var expected = new Matrix(3,  6,  9,
				  12, 15, 18,
				  21, 24, 27);
	matrixAlmostEqual(actual, expected);
});

test("In-place addition", function() {
	var actual = new Matrix(1, 2, 3,
				4, 5, 6,
				7, 8, 9);
	var other = new Matrix(2, 3, 4,
			       5, 6, 7,
			       8, 9, 10);
	actual.addInPlace(other);
	var expected = new Matrix(3, 5, 7,
				  9, 11, 13,
				  15, 17, 19);
	matrixAlmostEqual(actual, expected);
});
